const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OutputPrintPlugin = require('ad-ui-plugin-output-print')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')

const { resolve, isLocal, env } = require('./utils')
const package = require('../package.json')
const { description, version } = package
const {
  IS_LOCAL,
  GATEWAY,
  EP_COOKIE_DOMAIN,
  EP_PUBLIC_URL,
  EP_ZHOUSHAN_URL,
  EP_PORTAL_URL,
  EP_PASSPORT_URL,
} = require('ad-ui-dispose')(env)

const plugins = [
  new HtmlWebpackPlugin({
    title: description,
    template: resolve('webpack/template.html'),
    favicon: resolve('src/assets/favicon.ico'),
    filename: './index.html',
    showErrors: true,
    inject: 'body',
    minify: {
      removeComments: true,
      collapseWhitespace: true,
      removeAttributeQuotes: true
    }
  }),
  new webpack.DefinePlugin({
    '__DEBUG__': isLocal ? IS_LOCAL : false,
    '__GATEWAY__': '"' + GATEWAY + '"',
    '__COOKIE_DOMAIN__': '"' + EP_COOKIE_DOMAIN + '"',
    '__EP_PUBLIC_URL__': '"' + EP_PUBLIC_URL + '"',
    '__EP_ZHOUSHAN_URL__': '"' + EP_ZHOUSHAN_URL + '"',
    '__EP_PORTAL_URL__': '"' + EP_PORTAL_URL + '"',
    '__EP_PASSPORT_URL__': '"' + EP_PASSPORT_URL + '"',
    '__ACTIVE_REDUCEDR_TOOLS__': IS_LOCAL,
    '__CDN__': '"https://cdn.e-ports.com"',
  }),
]

if (!isLocal) {
  plugins.push(
    // @ts-ignore
    new MiniCssExtractPlugin({
      filename: 'css/[name]-[contenthash:6].css'
    }),
    new OptimizeCSSAssetsPlugin({ // 样式压缩
      cssProcessor: require('cssnano'), //引入cssnano配置压缩选项
      cssProcessorOptions: {
        discardComments: { removeAll: true }
      },
      canPrint: true //是否将插件信息打印到控制台
    }),
    new webpack.ContextReplacementPlugin(
      /moment[/\\]locale$/,
      /zh-cn/
    )
  )
} else {
  plugins.push(
    // @ts-ignore
    new OutputPrintPlugin({ name: description, version })
  )
}

module.exports = plugins
